package com.kaifamiao.util;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import com.kaifamiao.poker.Poker;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;

public class MailSendUtil {
	

	private static final MailAccount account = new MailAccount();
	
	private static List<String> names = new ArrayList<String>();
	
	static {
		// smtp.163.com
		account.setHost("smtp.qq.com");
		account.setPort(25);
		account.setAuth(true);
		account.setFrom("你的邮箱");
		account.setUser("你的邮箱");
		account.setPass("你的授权码");
	}
	
	public static void send() {
		List<String> players = getUsers();
		String playerCount = "本局游戏共有 " + players.size() + "人参与。";
		System.out.println(playerCount);
		List<String> cards = new Poker(players.size()).deal();
		
		Properties prop = new Properties();
		for (int i = 0; i < players.size(); i++) {
			// 玩家姓名和身份记录下来
			prop.setProperty(names.get(i), cards.get(i));
			String sf = "<b>" + cards.get(i) + "</b>";
			MailUtil.send(account, players.get(i), "开发喵-狼人杀发牌助手", "<h1>您本局的身份是：</h1> <p>" + sf + "</p>" + "<p>" + playerCount + "</p>" + "<b> 祝您游戏愉快！</b>", true);
		}
		
		try {
			// 记录玩家身份
			prop.store(new FileWriter("kfm.txt"),  "数据文件");
//			prop.store(new FileOutputStream("kfm.txt"),  "数据文件");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static List<String> getUsers(){
		InputStream in = MailSendUtil.class.getResourceAsStream("users.properties");
		Properties prop = new Properties();
		try {
			prop.load(in);
			String users = prop.getProperty("users");
			String name = prop.getProperty("names");
			names = Arrays.asList(name.split(","));
			return Arrays.asList(users.split(","));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
}
